﻿using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace Ecobee.Net
{
	public class CamelCaseStringEnumConverter : StringEnumConverter
	{
		public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
		{
			// ***
			// *** Convert the string version of the enum value
			// *** to Camel Case
			// ***
			writer.WriteValue(CamelCaseStringEnumConverter.ToCamelCase(value));
		}

		/// <summary>
		/// Converts a enum name that is assumed to be in Pascal Case into
		/// a string that is in Camel Case.
		/// </summary>
		/// <param name="enumValue"></param>
		/// <returns></returns>
		public static string ToCamelCase(object enumValue)
		{
			string returnValue = string.Empty;

			string stringValue = Enum.GetName(enumValue.GetType(), enumValue);
			returnValue = string.Format("{0}{1}", stringValue.Substring(0, 1).ToLower(), stringValue.Substring(1, stringValue.Length - 1));

			return returnValue;
		}
	}
}
